
package io.fredia.femicro.tool.search.lucene.util;

import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.search.highlight.Highlighter;

import io.fredia.femicro.api.vo.search.IndexObject;

/**
 * 文本工具
 * 
 * @author : Fredia
 * @since : 2018年3月16日
 * @version : v1.0.0
 */
public class DocumentUtil {

	public static Document IndexObject2Document(IndexObject indexObject) {
		Document doc = new Document();
		doc.add(new StoredField("id", indexObject.getId()));
		doc.add(new TextField("title", indexObject.getTitle(), Field.Store.YES));
		doc.add(new TextField("summary", indexObject.getKeywords(), Field.Store.YES));
		doc.add(new TextField("descripton", indexObject.getDescripton(), Field.Store.YES));
		doc.add(new StoredField("postDate", indexObject.getPostDate()));
		doc.add(new StoredField("url", indexObject.getUrl()));
		return doc;
	}

	public static IndexObject document2IndexObject(Analyzer analyzer, Highlighter highlighter, Document doc,
			float score) throws Exception {
		IndexObject indexObject = new IndexObject();
		indexObject.setId(Long.parseLong(doc.get("id")));
		indexObject.setTitle(stringFormatHighlighterOut(analyzer, highlighter, doc, "title"));
		indexObject.setKeywords(stringFormatHighlighterOut(analyzer, highlighter, doc, "summary"));
		indexObject.setDescripton(stringFormatHighlighterOut(analyzer, highlighter, doc, "descripton"));
		indexObject.setPostDate(doc.get("postDate"));
		indexObject.setUrl(doc.get("url"));
		indexObject.setScore(score);
		return indexObject;
	}

	/* 关键字加亮 */
	private static String stringFormatHighlighterOut(Analyzer analyzer, Highlighter highlighter, Document document,
			String field) throws Exception {
		String fieldValue = document.get(field);
		if (fieldValue != null) {
			TokenStream tokenStream = analyzer.tokenStream(field, new StringReader(fieldValue));
			return highlighter.getBestFragment(tokenStream, fieldValue);
		}
		return null;
	}
}